<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no" />
	<meta name="renderer" content="webkit" />
	<meta name="apple-mobile-web-app-capable" content="yes" />
	<meta name="apple-mobile-web-app-title" content="" />
	<meta name="format-detection" content="telephone=no,email=no,adress=no" />
	<link rel="stylesheet" type="text/css" href="css/test.css" />
	<script type="text/javascript" src="js/jquery-3.2.1.js"></script>
	<title>评价管理系统</title>
	<style>
		html,
		body {
			width: 100%;
			height: 100%;
			overflow: auto;
		}

		body {
			background: url(images/bg.png) no-repeat left top;
			background-size: 100%;
		}
	</style>
</head>


<body class="homeBg homeBgDetails">
	<header class="headTop">
		<p>评价管理系统</p>
		<img src="images/back.png" onclick="javascript:history.back(-1);">
	</header>
	<section class="contentNei">
		<!-- 内容开始 -->
		<article class="homeCenter">

			<!-- 02 -->
			<div class="homeJie homeJieDateils">
				<hgroup>
					<h6>请选择不满意选项</h6>
					<p>为了使我们能够更好的为您提供服务，请您对我们提出宝贵的意见</p>
				</hgroup>
			</div>

			<!-- 03 -->
			<div class="homeRule">
				<div class="homeRuleWhite homeRuleWhiteDeta">
					<!-- 中间的连接符 -->
					<div class="homeRuleLian"><img src="images/line.png"></div>
					<div class="homeRuleLianRight"><img src="images/line.png"></div>
				</div>

				<!-- 02 -->
				<div class="homeRuleWhite">
					<div class="homeDetaCheck">
						<ul id="checkList">
							<!-- <li class="zhong float">
								<input type="checkbox" value="1">
								<label>饭菜品种</label>
							</li>
							<li class="zhong float">
								<input type="checkbox" value="1">
								<label>饭菜份量</label>
							</li>
							<li class="zhong float">
								<input type="checkbox" value="1">
								<label>饭菜口味</label>
							</li>
							<li class="zhong float">
								<input type="checkbox" value="1">
								<label>卫生质量</label>
							</li> -->
						</ul>
					</div>
				</div>
			</div>
			<!-- 04 -->
			<button type="submit" class="detailsBtn" onclick="submit()">确认提交</button>
		</article>

	</section>


	<script type="text/javascript">
		var baseUrl = localStorage.getItem("baseUrl");
		console.log(baseUrl);
		var organ = JSON.parse(localStorage.getItem('organ'))
		console.log(organ)
		var ids = organ.appraiseItemIds.split(',');
		var names = organ.appraiseNames.split(',');
		var appraise = [];
		for (var index = 0; index < ids.length; index++) {
			appraise.push({
				id: ids[index],
				name: names[index]
			})
		}
		var todayDate = new Date();
		var yesterdayDate = new Date();
		yesterdayDate.setDate(yesterdayDate.getDate() - 1)
		
		var today = todayDate.getFullYear()+todayDate.getMonth()+todayDate.getDate();
		var yesterday = yesterdayDate.getFullYear()+yesterdayDate.getMonth()+yesterdayDate.getDate();
		console.log("昨天",yesterday)
		console.log("今天",today)

		var checkList = [];
		for (var index = 0; index < appraise.length; index++) {
			var element = appraise[index];
			checkList.push(
				`<li class="zhong float">
					<input type="checkbox" name="box" value="${element.id}">
					<label>${element.name}</label>
				</li>`
			)
		}
		$('#checkList').html(checkList.join(""))

		// 0满意 1一般 2不满意
			function satisfied(appraiseType){
				$.ajax({
					url: baseUrl+'/appraise',
					method:'put',
					data: {
						organId: organ.id,
						appraiseType: appraiseType
					},
					success: function (res) {
						console.log(res);
						if (res.code === 200) {
							$(window).attr('location', 'succeed.html');
						}
					}
				})
			}
		
		function submit() {
			var appraiseArr = JSON.parse(localStorage.getItem('appraise'));
			var tempArr = [];
			var isAppraise = false;
			if(appraiseArr){
				for(var i=0;i<appraiseArr.length;i++){
					var item = appraiseArr[i];
					if(item.time==today){
						tempArr.push(item);
					}
					if(item.time==today && item.organId==organ.id){
						isAppraise = true;
					}
				}
				localStorage.setItem("appraise",JSON.stringify(tempArr))
			}
			if(isAppraise){
				alert("今日已评价")
				return;
			}
		
			var data = $('input:checkbox[name="box"]:checked')
			var checkIds = [];
			for (var index = 0; index < data.length; index++) {
				var element = parseInt($(data[index]).val());
				checkIds.push(element);
			}
			console.log(checkIds);
			if(checkIds.length===0){
				alert("请至少选择一项!")
				return;
			}
			$.ajax({
				url: baseUrl + '/unsatisfactory',
				method: 'put',
				data: {
					organId: organ.id,
					appraiseIds: checkIds
				},
				success:function(res){
					console.log(res);
					if(res.success){
						//时间+机构id+评价等级（0满意 1一般 2不满意）
						var appraise = {
							time:today,
							organId:organ.id,
							choose:2
						}
						tempArr.push(appraise)
						localStorage.setItem('appraise',JSON.stringify(tempArr))
						satisfied(2);
					}
				}
			})
		}
	</script>

</body>

</html>